package com.tools.web.interceptor.auth;

import com.tools.common.container.OwnPairs;
import com.tools.common.object.Note;

import java.util.Map;
import java.util.Objects;

/**
 * 用户的登录记录的用户认证包装类
 * */
@Note("用户的登录记录的用户认证包装类")
public final class SingleAuthUser extends GenericAuthUser {

    @Note("设备类型 ID")
    private String deviceTypeId;

    @Note("用户登录记录的标识")
    private String uuid;

    @Note("用户类型标识。（比如平台用户、小程序用户等等）")
    private String userType;

    /* **************************************************************************************
     *
     *          构造器
     *
     * **************************************************************************************
     * */
    
    private SingleAuthUser() {
        super();
    }

    public SingleAuthUser(Object userId, Object user,
                         Map roleKeyMap, Map permissionIds,
                         Iterable<?> routers) {
        super(userId, user, roleKeyMap, permissionIds, routers);
    }

    /* **************************************************************************************
     *
     *          Getter 等方法
     *
     * **************************************************************************************
     * */

    @Override
    public String getUuid() {
        return uuid;
    }

    @Override
    public void setUuid(String uuid) {
        if(uuid == null || uuid.isEmpty()) {
            throw new NullPointerException("“用户登录记录的标识” 为空");
        }
        this.uuid = uuid;
    }

    @Override
    public String getDeviceTypeId() {
        return deviceTypeId;
    }

    @Override
    public void setDeviceTypeId(String deviceTypeId) {
        this.deviceTypeId = LoginDeviceTypeEnum.getEnum(deviceTypeId).getId();
    }

    @Override
    public String getUserType() {
        return (userType == null) ? "" : userType;
    }

    @Override
    public void setUserType(String userType) {
        this.userType = (userType == null) ? "" : userType;
    }

    @Override
    protected String protected_toStringPropertyString() {
        return ", deviceTypeId=" + deviceTypeId +
                ", uuid=" + uuid +
                ", userType=" + this.getUserType();
    }

    @Override
    public String toString() {
        return "SingleAuthUser{" +
                super.toString() +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        SingleAuthUser authUser = (SingleAuthUser) o;
        return Objects.equals(deviceTypeId, authUser.deviceTypeId) && Objects.equals(uuid, authUser.uuid) && Objects.equals(userType, authUser.userType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), deviceTypeId, uuid, userType);
    }

    /* **************************************************************************************
     *
     *          自定义方法与实现的方法
     *
     * **************************************************************************************
     * */

    @Note("拷贝当前 uuid 到新的认证对象 targetAuthUser")
    public void copyUuid(SingleAuthUser targetAuthUser) {
        targetAuthUser.uuid = this.uuid;
    }

    @Note("拷贝当前设备类型 ID 到新的认证对象 targetAuthUser")
    public void copyDeviceTypeId(SingleAuthUser targetAuthUser) {
        targetAuthUser.deviceTypeId = this.deviceTypeId;
    }

    public void copyUserType(SingleAuthUser targetAuthUser) {
        targetAuthUser.userType = this.getUserType();
    }

    @Note("拷贝当前 uuid 和设备类型 ID 到新的认证对象 targetAuthUser")
    public void copyAdditional(SingleAuthUser targetAuthUser) {
        targetAuthUser.uuid = this.uuid;
        targetAuthUser.deviceTypeId = this.deviceTypeId;
        targetAuthUser.userType = this.getUserType();
    }
    
    @Override
    public SingleAuthUser cloneAuthUser() {
        SingleAuthUser newAuthUser = new SingleAuthUser();
        super.copyBaseUserDataTo(newAuthUser);
        super.copyCustomDataMapTo(newAuthUser);
        return newAuthUser;
    }


    /* **************************************************************************************
     *
     *          重写的链式方法
     *
     * **************************************************************************************
     * */

    @Override
    public SingleAuthUser uuid(String uuid) {
        this.setUuid(uuid);
        return this;
    }

    @Override
    public SingleAuthUser deviceTypeId(String deviceType) {
        this.setDeviceTypeId(deviceType);
        return this;
    }

    @Override
    public SingleAuthUser userType(String userType) {
        this.setUserType(userType);
        return this;
    }


    @Override
    public SingleAuthUser userId(Object userId) {
        super.setUserId(userId);
        return this;
    }

    @Override
    public SingleAuthUser user(Object user) {
        super.setUser(user);
        return this;
    }

    @Override
    public SingleAuthUser userTypeClass(String userTypeClassName) {
        super.setUserTypeClass(userTypeClassName);
        return this;
    }

    @Override
    public SingleAuthUser roleKeyMap(Map roleKeyMap) {
        super.setRoleKeyMap(roleKeyMap);
        return this;
    }

    @Override
    public SingleAuthUser permissionKeyMap(Map permissionKeyMap) {
        super.setPermissionKeyMap(permissionKeyMap);
        return this;
    }

    @Override
    public SingleAuthUser routerList(Iterable routerList) {
        super.setRouterList(routerList);
        return this;
    }

    /* **************************************************************************************
     *
     *          自定义数据容器的功能方法
     *
     * **************************************************************************************
     * */

    @Override
    public SingleAuthUser putData(String name, Object value) {
        super.putData(name, value);
        return this;
    }

    @Override
    public SingleAuthUser putAllData(Map customData) {
        super.putAllData(customData);
        return this;
    }

    @Override
    public SingleAuthUser putAllData(OwnPairs customData) {
        super.putAllData(customData);
        return this;
    }

    @Override
    public SingleAuthUser putAllData(String jsonObject) {
        super.putAllData(jsonObject);
        return this;
    }

    @Override
    public SingleAuthUser putAllData(Object javaBean) {
        super.putAllData(javaBean);
        return this;
    }

    @Override
    public SingleAuthUser removeData(String name) {
        super.removeData(name);
        return this;
    }

    @Override
    public SingleAuthUser removeAllData(String... names) {
        super.removeAllData(names);
        return this;
    }

    @Override
    public SingleAuthUser removeAllData(Iterable<String> names) {
        super.removeAllData(names);
        return this;
    }

    @Override
    public SingleAuthUser clearData() {
        super.clearData();
        return this;
    }

}
